package com.gec.shop;

import java.io.Serializable;
import java.util.Scanner;

//商品类
public class Commodity implements Serializable {

    private int id;//商品id
    private String name;//商品名字
    private double price;//商品单价
    private int quantity;//商品数量
    //用于修改商品时使用
    private int newid;//新商品id

    public Commodity() {
    }

    public Commodity(int id,String name,double price,int quantity) {
        this.name = name;
        this.id = id;
        this.quantity = quantity;
        this.price = price;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public int getQuantity() {
        return quantity;
    }

    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getNewid() {
        return newid;
    }

    public void setNewid(int newid) {
        this.newid = newid;
    }

    public static Commodity newCommodity(Scanner scanner){
        Commodity commodity = new Commodity();
        while(true){
            try {
                System.out.print("添加的商品名称->");
                commodity.setName(scanner.next());
                System.out.print("添加的商品单价->");
                commodity.setPrice(scanner.nextDouble());
                System.out.print("添加的商品数量->");
                commodity.setQuantity(scanner.nextInt());
                return commodity;
            }catch (Exception e){
                e.printStackTrace();
                System.out.println("输入格式有误");
            }
        }
    }
    public static Commodity shoppingAddCommodity(Scanner scanner){
        Commodity commodity = new Commodity();
        while(true){
            try{
            System.out.println("需要添加购物车的商品ID->");
            commodity.setId(scanner.nextInt());
            System.out.println("需要添加购物车的商品数量->");
            commodity.setQuantity(scanner.nextInt());
            return  commodity;
            }catch (Exception e){
                e.printStackTrace();
                System.out.println("输入格式有误");
            }
        }
    }
    public static Commodity shoppingdelectCommodity(Scanner scanner){
        Commodity commodity = new Commodity();
        while(true){
            try{
                System.out.println("需要删除购物车的商品ID->");
                commodity.setId(scanner.nextInt());
                System.out.println("需要删除购物车的商品数量->");
                commodity.setQuantity(scanner.nextInt());
                return  commodity;
            }catch (Exception e){
                e.printStackTrace();
                System.out.println("输入格式有误");
            }
        }
    }
    public static Commodity shoppingXgCommodity(Scanner scanner){
        Commodity commodity = new Commodity();
        while(true){
            try{
                System.out.println("需要修改购物车的商品ID->");
                commodity.setId(scanner.nextInt());
                System.out.println("需要修改购物车的商品数量->");
                commodity.setQuantity(scanner.nextInt());
                return  commodity;
            }catch (Exception e){
                e.printStackTrace();
                System.out.println("输入格式有误");
            }
        }
    }
    public static Commodity modityCommdity(Scanner scanner) {
        Commodity commodity = new Commodity();
        while(true){
            try {
                System.out.println("需要修改的商品id");
                commodity.setId(scanner.nextInt());
                System.out.print("修改的商品名称->");
                commodity.setName(scanner.next());
                System.out.print("修改的商品单价->");
                commodity.setPrice(scanner.nextDouble());
                System.out.print("修改的商品数量->");
                commodity.setQuantity(scanner.nextInt());
                return commodity;
            }catch (Exception e){
                e.printStackTrace();
                System.out.println("输入格式有误");
            }
        }
    }
}
